package com.vessel.decoration.mvp.ui.activity.me.manage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportActivity;
import com.vessel.decoration.app.data.entity.ArtisanInfoResponse;
import com.vessel.decoration.di.component.mine.manage.DaggerSkillComponent;
import com.vessel.decoration.di.module.mine.manage.SkillModule;
import com.vessel.decoration.mvp.contract.mine.manage.SkillContract;
import com.vessel.decoration.mvp.presenter.mine.manage.SkillPresenter;

import butterknife.BindView;
import butterknife.OnClick;

import static com.vessel.decoration.app.constant.Constants.TYPE;
import static com.vessel.decoration.app.constant.Constants.TYPE_ADD;
import static com.vessel.decoration.app.constant.Constants.TYPE_UPDATE;

/**
 * @author vesselzhang
 * @date 2017/12/8
 */
@Route(path = "/app/worker/skill")
public class MeWorkerSkillActivity extends MySupportActivity<SkillPresenter> implements SkillContract.View {

    @BindView(R.id.tv_title)
    TextView mTitleTV;
    @BindView(R.id.tv_right)
    TextView mTitleRightTV;
    @BindView(R.id.product_name)
    EditText mNameEV;
    @BindView(R.id.product_price)
    EditText mPriceEV;
    @BindView(R.id.product_remark)
    EditText mRemarkEV;
    @BindView(R.id.product_image)
    ImageView mImageIV;
    @BindView(R.id.product_submit)
    Button mSubmitBt;

    private int type;
    private ArtisanInfoResponse.SkillsBean skill;
    private AppComponent mAppComponent;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSkillComponent
                .builder()
                .appComponent(appComponent)
                .skillModule(new SkillModule(this))
                .build()
                .inject(this);
        this.mAppComponent = appComponent;
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_me_worker_skill;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleTV.setText("技能管理");
        type = getIntent().getIntExtra(TYPE, TYPE_ADD);
        switch (type) {
            case TYPE_ADD:
                mSubmitBt.setText("添加");
                break;
            case TYPE_UPDATE:
                mSubmitBt.setText("更新");
                mTitleRightTV.setVisibility(View.VISIBLE);
                mTitleRightTV.setText("删除");
                mTitleRightTV.setOnClickListener(view -> mPresenter.delete(skill.getSkillId()));
                skill = (ArtisanInfoResponse.SkillsBean) getIntent().getSerializableExtra("data");
                if (skill == null) {
                    showMessage("错误的传参！");
                    killMyself();
                    return;
                }
                mNameEV.setText(skill.getSkillName());
                mPriceEV.setText("" + skill.getPrice());
                mRemarkEV.setText(skill.getRemark());
                if (!TextUtils.isEmpty(skill.getSkillPic())) {
                    showPic(skill.getSkillPic());
                }
                break;
        }
    }

    @OnClick({R.id.product_image_add, R.id.product_submit, R.id.iv_left})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.product_image_add:
                mPresenter.addImage();
                break;
            case R.id.product_submit:
                mPresenter.submit(type == TYPE_ADD ? -1 : skill.getSkillId(), mNameEV.getText().toString()
                        , mPriceEV.getText().toString(), mRemarkEV.getText().toString());
                break;
            case R.id.iv_left:
                killMyself();
                break;
        }
    }

    @Override
    public void showPic(String uri) {
        mImageIV.setVisibility(View.VISIBLE);
        mPresenter.setUri(uri);
        mAppComponent.imageLoader()
                .loadImage(activity, ImageConfigImpl
                        .builder()
                        .imageView(mImageIV)
                        .url(BuildConfig.APP_DOMAIN + uri)
                        .build());
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        ArmsUtils.makeText(activity, message);
    }

    @Override
    public void launchActivity(Intent intent) {

    }

    @Override
    public void killMyself() {
        finish();
    }
}
